if exists (select 1 from sysobjects where name = 'set_termin' and type = 'P')
begin
   drop procedure set_termin
   print 'Procedure: set_termin deleted ...'
end
go
create procedure set_termin(
  @terminname        varchar(32)   = 'Sitzung',
  @kurztext          varchar(60)   = 'Sitzung test',
  @beschreibung      varchar(250)  = 'Sitzung besch test',
  @rgbin             int           = 0,
  @colorname         varchar(32)   = 'white',
  @shgin             char(1)       = 'S', -- S: Schrift   H: Hintergrund
  @mandid            char(2)       = 'li'
)
as
begin
  set nocount on
  declare @beschrid   int
  if @beschreibung is null select @beschreibung = ''
  if not exists (select 1 from Beschreibung where Beschreibung.BeschrID between 1501 and 2000)
  begin
    select @beschrid = 1501
  end
  else
  begin
    select @beschrid = max(Beschreibung.BeschrID) + 1
      from Beschreibung
     where Beschreibung.BeschrID between 1501 and 2000
  end
  
  exec set_beschreibung @beschrid, @kurztext, @beschreibung
  
  insert into Termin (Name, BeschrID, Status, DefRGB, Colorname, SHG, mandid) values (@terminname, @beschrid, 1, @rgbin, @colorname, upper(@shgin), @mandid)
  
end
go
print 'Procedure: set_termin done ...'
go
grant exec on set_termin to prsadmins with grant option
go
grant exec on set_termin to prsusers
go

